import * as util from '@/common/utils/common'
import { createColumn, updateColumn, getColumn , fetchTreeGridData} from '@/api/ColumnApi'
import EditBase from '@/components/TableGrid/editBase'

export default {
  extends: EditBase,
  name: 'columnEdit',
  data() {
    return {
      domainName: '栏目',
      editFormId: 'columnEditFormId',
      formData: {},
      rules: {
				name: [{ required: true, message: '名称不能为空', trigger: 'blur' }
				],
        position: [{ required: true, message: '排序不能为空', trigger: 'blur' }
				],
      },
      //以下是树相关数据
      parentTreeData: [],
      parentTreeDefaultProps: {
        children: 'children',
        label: 'label'
      },
      singleSelectTreeId: undefined,
      treeCheckNum: 0,// 用于树单选
    }
  },
  methods: {
    initDataFun(id) {
      this.initTreeGridData();
      return getColumn(id);
    },
    initCreate() {
      this.initTreeGridData();
    },
    createDataFun() {
      let parentTreeCheckedKeys = this.$refs.treeForm.getCheckedKeys();
      if (parentTreeCheckedKeys.length !== 0){
        this.formData.parentId = parentTreeCheckedKeys[0];
      }
      this.formData.type = 'draw';
      return createColumn(this.formData);
    },
    updateDataFun() {
      let parentTreeCheckedKeys = this.$refs.treeForm.getCheckedKeys();
      if (parentTreeCheckedKeys.length !== 0){
        this.formData.parentId = parentTreeCheckedKeys[0];
      }else{
        this.formData.parentId = '-1';
      }
      this.formData.type = 'draw';
      const tempData = Object.assign({}, this.formData)
      return updateColumn(tempData);
    },
    initTreeGridData() {
      fetchTreeGridData().then(response => {
        if (util.isRequestSuccess(response)) {
          this.parentTreeData = util.getResponseContent(response)
        }
      });
    },
    handleParentTreeClick(data,checked, node) {
      this.treeCheckNum++;
      if(this.treeCheckNum%2==0){
        if(checked){
          this.$refs.treeForm.setCheckedNodes([]);
          this.$refs.treeForm.setCheckedNodes([data]);
          //交叉点击节点
        }else{
          this.$refs.treeForm.setCheckedNodes([]);
          //点击已经选中的节点，置空
        }
      }
    },
  }
}
